import json
from rest_framework import serializers
from .models import User,Org
"""
User序列化器
"""

class Orgserializers(serializers.Serializer):
    name = serializers.CharField(max_length=50)


class Userserializers(serializers.Serializer):
    name = serializers.CharField(max_length=20)
    empno = serializers.IntegerField()
    group = Orgserializers(many=True,read_only=True)
    group_list = serializers.CharField(write_only=True)
    def create(self,data):
        print(data)
        group_list = json.loads(data['group_list'])
        u = User.objects.create(
            name = data['name'],
            empno = data['empno']
        )
        u.group.add(*group_list)
        return u




    

